package com.guigusuqi.commonutils.utils;

import lombok.Data;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
@EnableAsync // 开启多线程
@Data
public class ThreadPoolConfig
{
    public ScheduledThreadPoolExecutor scheduledThreadPoolExecutor;

    public ThreadPoolConfig()
    {
        scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(5);
        //  设置最大线程数
        scheduledThreadPoolExecutor.setMaximumPoolSize(20);
        //   设置线程活跃时间（秒）
        scheduledThreadPoolExecutor.setKeepAliveTime(60,TimeUnit.SECONDS);
        //  当最大池被填满时，此策略为我们提供可伸缩队列
        scheduledThreadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    }
}
